package com.example.foobar.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.foobar.business.service.DyCommentService;
import com.example.foobar.business.service.DyService;
import com.example.foobar.business.service.DyVideoService;
import com.example.foobar.entity.DaNewCommerCount;
import com.example.foobar.entity.DyComment;
import com.example.foobar.entity.DyVideo;
import com.example.foobar.vo.DyInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class DyServiceImpl implements DyService {

    public static final int ACTIVE_STATUS = 1;

    @Autowired
    DyCommentService dyCommentService;

    @Autowired
    DyVideoService dyVideoService;

    @Override
    public DyInfoVO getDyInfo(String uniacid) {
        LambdaQueryWrapper<DyVideo> dyVideoLambdaQueryWrapper = new LambdaQueryWrapper<>(DyVideo.class)
                .eq(DyVideo::getUniacid, uniacid).eq(DyVideo::getStatus, ACTIVE_STATUS);
        List<DyVideo>  dyVideoList = dyVideoService.list(dyVideoLambdaQueryWrapper);

        LambdaQueryWrapper<DyComment> dyCommentLambdaQueryWrapper = new LambdaQueryWrapper<>(DyComment.class)
                .eq(DyComment::getUniacid, uniacid).eq(DyComment::getStatus, ACTIVE_STATUS);
        List<DyComment>  dyCommentList = dyCommentService.list(dyCommentLambdaQueryWrapper);

        DyVideo dyVideo = dyVideoList.get(getRandom(dyVideoList.size()));
        DyComment dyComment = dyCommentList.get(getRandom(dyCommentList.size()));

        DyInfoVO dyInfoVO = new DyInfoVO();
        dyInfoVO.setUnicaid(uniacid);
        dyInfoVO.setComment(dyComment.getComment());
        dyInfoVO.setVideoUrl(dyVideo.getUrl() + "?attname=video.mp4");

        return dyInfoVO;
    }

    private int getRandom(int max){
        Random r = new Random();
        return r.nextInt(max);
    }
}
